package com.anolesoft.yr.mbus.prod.model.service.impl;

import com.anolesoft.yr.mbus.prod.model.dto.ProductionTypeDTO;
import com.anolesoft.yr.mbus.prod.model.entity.ProductionType;
import com.anolesoft.yr.mbus.prod.model.mapper.ProductionTypeMapper;
import com.anolesoft.yr.mbus.prod.model.service.ProductionTypeService;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 谭明洋
 */

@Service
public class ProductionTypeServiceImpl implements ProductionTypeService {
    @Autowired
    private ProductionTypeMapper prodTypeMapper;

    @Transactional
    @Override
    public int add(ProductionTypeDTO prodTypeDTO) {
        return prodTypeMapper.addProduction(
                ProductionType.builder()
                .objuid(prodTypeDTO.getObjuid())
                .typeCode(prodTypeDTO.getTypeCode())
                .typeName(prodTypeDTO.getTypeName())
                .typeNature(prodTypeDTO.getTypeNature())
                .remark(prodTypeDTO.getRemark())
                .cTime(prodTypeDTO.getCTime())
                .cUserUid(prodTypeDTO.getCUserUid())
                .delFlag(prodTypeDTO.getDelFlag())
                .facCode(prodTypeDTO.getFacCode())
                .mTime(prodTypeDTO.getMTime())
                .mUserUid(prodTypeDTO.getMUserUid())
                .build());
    }



    @Transactional
    @Override
    public int delete(String id) {
        return prodTypeMapper.deleteProductionById(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        int i = 0;
        for (String id : ids) {
            i += prodTypeMapper.deleteProductionById(id);
        }
        return i;
    }

    @Transactional
    @Override
    public int update(ProductionTypeDTO prodTypeDTO) {
        return prodTypeMapper.updateProduction(dto2entity(prodTypeDTO));
    }

    private ProductionType dto2entity(ProductionTypeDTO prodTypeDTO) {
        return ProductionType.builder()
                .objuid(prodTypeDTO.getObjuid())
                .typeCode(prodTypeDTO.getTypeCode())
                .typeName(prodTypeDTO.getTypeName())
                .typeNature(prodTypeDTO.getTypeNature())
                .remark(prodTypeDTO.getRemark())
                .cTime(prodTypeDTO.getCTime())
                .cUserUid(prodTypeDTO.getCUserUid())
                .delFlag(prodTypeDTO.getDelFlag())
                .facCode(prodTypeDTO.getFacCode())
                .mTime(prodTypeDTO.getMTime())
                .mUserUid(prodTypeDTO.getMUserUid())
                .build();
    }

    @Override
    public Optional<ProductionTypeDTO> queryProduction(String id) {
        ProductionTypeDTO prodTypeDTO1 = entity2dto(prodTypeMapper.queryProductionById(id));
        if (prodTypeDTO1 != null) {
            return Optional.of(prodTypeDTO1);
        }
        return Optional.empty();
    }

    @Override
    public List<ProductionTypeDTO> queryProductions() {
        List<ProductionTypeDTO> prodTypeDTOS = prodTypeMapper.queryProductions().stream().map(t -> entity2dto(t)).collect(Collectors.toList());
        return prodTypeDTOS;
    }

    @Override
    public PageData<List<ProductionTypeDTO>> queryPageAll(Integer limit, Integer current) {
        Page<ProductionType> page = new Page<>(current, limit);
        List<ProductionType> prodTypeDTOS = prodTypeMapper.selectPageAll(page);
        List<ProductionTypeDTO> collect = prodTypeDTOS.stream().map(t -> entity2dto(t)).collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), collect);
    }

    private ProductionTypeDTO entity2dto(ProductionType production) {
        return ProductionTypeDTO.builder()
                .objuid(production.getObjuid())
                .remark(production.getRemark())
                .typeCode(production.getTypeCode())
                .typeName(production.getTypeName())
                .typeNature(production.getTypeNature())
                .facCode(production.getFacCode())
                .cTime(production.getCTime())
                .cUserUid(production.getCUserUid())
                .delFlag(production.getDelFlag())
                .mTime(production.getMTime())
                .mUserUid(production.getMUserUid())
                .build();
    }

    @Override
    public List<ProductionTypeDTO> queryList(ProductionTypeDTO prodTypeDTO) {
        return prodTypeMapper.queryList(dto2entity(prodTypeDTO)).stream().map(t ->entity2dto(t)).collect(Collectors.toList());
    }

    @Override
    public PageData<List<ProductionTypeDTO>> queryListPage(ProductionTypeDTO prodTypeDTO, Integer rows, Integer page) {
        Page<ProductionType> page1 = new Page<>(page, rows);
        ProductionType productionType = dto2entity(prodTypeDTO);
        List<ProductionType> productionTypes = prodTypeMapper.queryListPage(productionType, page1);
        List<ProductionTypeDTO> collect = productionTypes.stream().map(t -> entity2dto(t)).collect(Collectors.toList());
        return new PageData<>((int)page1.getTotal(),collect);
    }

    @Override
    public PageData<List<ProductionTypeDTO>> queryComboxList(ProductionTypeDTO prodTypeDTO, Integer rows, Integer page) {
        Page<ProductionType> page1 = new Page<>(page,rows);
        List<ProductionTypeDTO> prodTypeDTOS = prodTypeMapper.queryComboxList(dto2entity(prodTypeDTO), page1).stream().map(t -> entity2dto(t)).collect(Collectors.toList());
        return new PageData<>((int)page1.getTotal(),prodTypeDTOS);
    }
}
